package com.kuangxiaoyang.foodapp;

import androidx.appcompat.app.AppCompatActivity;

import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ListView;
import android.widget.TextView;

import com.example.foodclient.adapter.CommentsAdapter;
import com.example.foodclient.pojo.CommentBean;
import com.google.gson.Gson;

import java.io.IOException;
import java.util.List;

import okhttp3.OkHttpClient;
import okhttp3.Request;
import okhttp3.Response;

public class CommentsListActivity extends AppCompatActivity {

    private static final String TAG = "CommentsListActivity";

    private ListView commentlistview;
    private List<CommentBean.DataBean> data;
    private TextView foodNameTv;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_comments);
        initView();
        showComments();
        initEvent();
    }

    private void initEvent() {
        commentlistview.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                Intent intent = new Intent(CommentsListActivity.this,CommentDetailActivity.class);
                intent.putExtra("id",String.valueOf(data.get(position).getId()));
                intent.putExtra("position",position);
                startActivity(intent);
            }
        });
    }

    private void showComments() {
        Intent intent = getIntent();
        final String foodId = intent.getStringExtra("foodId");
        new Thread(new Runnable() {
            @Override
            public void run() {
                try {
                    OkHttpClient client = new OkHttpClient().newBuilder()
                            .build();
                    Request request = new Request.Builder()
                            .url("http://192.168.137.1:8083/foodserver/comment/list/" + foodId)
                            .method("GET", null)
                            .build();

                    Response response = client.newCall(request).execute();
                    String result = response.body().string();
                    Log.e(TAG, "run: " + result);
                    Gson gson = new Gson();
                    data = gson.fromJson(result, CommentBean.class).getData();

                    runOnUiThread(new Runnable() {
                        @Override
                        public void run() {
                            commentlistview.setAdapter(new CommentsAdapter(CommentsListActivity.this, data));
                            Intent intent = getIntent();
                            String foodName = intent.getStringExtra("foodName");
                            foodNameTv.setText(foodName);
                        }
                    });

                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }).start();
    }

    private void initView() {
        commentlistview = findViewById(R.id.comments_list_view);
        foodNameTv = findViewById(R.id.cm_food_name_tv);
    }
}
